/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik;

import java.awt.Color;

import javax.swing.SwingUtilities;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;

import ch.bfh.ti.kybernetik.gui.common.RoboterColorManager;
import ch.bfh.ti.kybernetik.gui.slick.GuiSimulator;
import ch.bfh.ti.kybernetik.gui.swing.GuiCommand;
import ch.bfh.ti.kybernetik.simulator.controller.Simulator;
import ch.bfh.ti.kybernetik.simulator.controller.lightBulb.LightBulbController;
import ch.bfh.ti.kybernetik.simulator.controller.roboter.RoboterController;
import ch.bfh.ti.kybernetik.simulator.controller.roboter.RobotorControllerFactory;
import ch.bfh.ti.kybernetik.simulator.model.LightBulb;
import ch.bfh.ti.kybernetik.simulator.model.Roboter;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws SlickException {

		final RoboterController[] roboterControllers = buildRobotorControllers();
		final LightBulbController[] lightBulbControllers = buildLightBulbControllers();
		final Simulator simulator = buildSimulator(roboterControllers, lightBulbControllers);
		
		

		// Start simulator Thread
		Thread t = new Thread(simulator);
		t.setName("simulatorThread");
		t.start();

		// Start Swing GUI Controller Thread
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GuiCommand ex = new GuiCommand(simulator);
				ex.setVisible(true);
			}
		});

		// Start Slick GUI
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {

				try {
					AppGameContainer app = new AppGameContainer(new GuiSimulator(simulator));
					app.setDisplayMode(1000, 750, false);
					// app.setFullscreen(true);
					app.start();
				} catch (SlickException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		t2.setName("slickThread");
		t2.start();

	}

	private static LightBulbController[] buildLightBulbControllers() {
		final LightBulbController[] lightBulbControllers = new LightBulbController[] { new LightBulbController(new LightBulb(500, 0)) };
		return lightBulbControllers;
	}

	private static RoboterController[] buildRobotorControllers() {
		RoboterColorManager roboterColor = RoboterColorManager.getInstance();
		RoboterController[] roboterControllers = new RoboterController[1];
		for (int i = 0; i < roboterControllers.length; i++) {
			roboterControllers[i] = RobotorControllerFactory.createRoboterController();
			roboterColor.putRoboterController(roboterControllers[i].getRoboter(), Color.cyan);
		}
		return roboterControllers;
	}

	private static Simulator buildSimulator(RoboterController[] roboterControllers, final LightBulbController[] lightBulbControllers) {
		final Simulator simulator = new Simulator(lightBulbControllers, roboterControllers);
		return simulator;
	}
}
